package com.link.play.fit.ai.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.KeyEvent
import android.view.WindowManager
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.DialogFoodDatabaseAddFoodBinding
import com.link.play.fit.ai.databinding.DialogSettingDeleteUserBinding

//添加食物弹窗
class DeleteUserDialog(var openEditFood: () -> Unit) {
    private var dialog: Dialog? = null


    //    fun build(context: Context) {
//        val iv = ImageView(context)
//        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
//        val param = WindowManager.LayoutParams()
//        param.x = 100
//        param.y = 200
//        wm.addView(iv, param)
//    }
    fun build(context: Activity) {
        val binding = DialogSettingDeleteUserBinding.inflate(
            context.layoutInflater
        )
        dialog = Dialog(context)
        dialog!!.setContentView(binding.root)
        val window = dialog?.window
        window?.run {
            setBackgroundDrawable(ColorDrawable())
            setDimAmount(0.4f)
            setGravity(Gravity.CENTER)
            val layoutParams = attributes
            //设置dialog可以穿透并且有蒙层
            layoutParams.flags =
                    //WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_DIM_BEHIND
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
//            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            // layoutParams.verticalMargin = 0.06f
            attributes = layoutParams
            decorView.setPadding(0, 0, 0, 0)
        }
        binding.btnCancel.setOnClickListener {
            dialog?.apply { dismiss() }
        }
        binding.btnDelete.setOnClickListener {
            openEditFood.invoke()
            dialog?.apply { dismiss() }
        }
        binding.constraint.setOnClickListener {
        }
        binding.root.setOnClickListener {
            dialog?.apply { dismiss() }
        }
        dialog?.apply {
            setOnKeyListener { a, b, c ->
                if (b == KeyEvent.KEYCODE_BACK && c.action == KeyEvent.ACTION_UP) {
                    a.dismiss()
                    true
                } else {
                    false
                }
            }
        }
    }

    fun show() {
        dialog?.apply {
            show()
        }
    }
    fun dismiss(){
        dialog?.apply {
            dismiss()
        }
    }
}